/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.jiujiudevice.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.jiujiudevice.entity.JiuInk;
import com.boxin.ims.modules.jiujiudevice.service.JiuInkService;

/**
 * 颜料配置Controller
 * @author sxh
 * @version 2018-10-08
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/jiujiudevice/jiuInk")
public class JiuInkController extends BaseController {

	@Autowired
	private JiuInkService jiuInkService;
	
	@ModelAttribute
	public JiuInk get(@RequestParam(required=false) Long id) {
		if (id != null){
		return jiuInkService.get(id);
		}else{
			return new JiuInk();
		}
	  }
	
	@RequiresPermissions("jiujiudevice:jiuInk:view")
	@RequestMapping(value = {"list", ""})
	public String list(JiuInk jiuInk, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		System.out.println("deviceSn====>"+jiuInk.getDeviceSn());
        Page<JiuInk> page = jiuInkService.find(new Page<JiuInk>(request, response), jiuInk); 
        model.addAttribute("page", page);
		return "modules/jiujiudevice/jiuInkList";
	}

	@RequiresPermissions("jiujiudevice:jiuInk:view")
	@RequestMapping(value = "form")
	public String form(JiuInk jiuInk, Model model) {
	
		User user = UserUtils.getUser();
		model.addAttribute("jiuInk", jiuInk);
		return "modules/jiujiudevice/jiuInkForm";
	}

	@RequiresPermissions("jiujiudevice:jiuInk:edit")
	@RequestMapping(value = "save")
	public String save(JiuInk jiuInk, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, jiuInk)){
			return form(jiuInk, model);
		}
		JiuInk j  = jiuInkService.getBySnAndName(jiuInk.getDeviceSn(),jiuInk.getName());
		if(j != null && j.getId() >0) {
			//修改
			j.setCreateTime(new Date());
			j.setName(jiuInk.getName());
			j.setColor(jiuInk.getColor());
			j.setPrice(jiuInk.getPrice());
			j.setDeviceSn(jiuInk.getDeviceSn());
			jiuInkService.save(j);
			addMessage(redirectAttributes, "修改路线配置【'" + jiuInk.getName() + "'】成功");
		}else{
			jiuInkService.save(jiuInk);
			addMessage(redirectAttributes, "保存路线配置【'" + jiuInk.getName() + "'】成功");
		}
		addMessage(redirectAttributes, "保存路线配置【'" + jiuInk.getName() + "'】成功");
		return "redirect:"+Global.ADMIN_PATH+"/jiujiudevice/jiuInk?deviceSn="+jiuInk.getDeviceSn();
	}
	
	@RequiresPermissions("jiujiudevice:jiuInk:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		jiuInkService.delete(id);
		addMessage(redirectAttributes, "删除路线配置成功");
		return "redirect:"+Global.ADMIN_PATH+"/jiujiudevice/jiuInk/?repage";
	}

}
